package model.client;

import com.alibaba.fastjson.JSONObject;

import java.io.ObjectOutputStream;
import java.net.Socket;

public class ClientHeart implements Runnable {
    private ObjectOutputStream out;
    private Client client;
    private Socket socket;

    public ClientHeart(Socket socket, ObjectOutputStream out, Client client) {
        this.socket = socket;
        this.out = out;
        this.client = client;
    }

    @Override
    public void run() {
        try {
            System.out.println("***心跳包线程启动***");

            while (true) {
                Thread.sleep(5*1000);
                JSONObject object = new JSONObject();
                object.put("type", "6");
                object.put("msg", "200");
                out.writeObject(object);
                out.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                socket.close();
                client.setConnect_state(false);
                client.reconnect();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}
